<!--
 * @Date: 2022-04-09 14:48:19
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-07-15 10:07:29
 * @FilePath: \imitation-microblog-vue\src\components\settingClient.vue
 * @Description: something
-->
<template>
  <div class="passwordSetting">
    <div>账号信息设置</div>
    <div class="InputArea">
      <el-form
        label-position="top"
        label-width="100px"
        style="max-width: 460px"
        :model="settingClient"
        :rules="rules"
        ref="ruleFormRef"
      >
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="settingClient.password"
            maxlength="14"
            type="password"
          />
          <i class="tips">最多14个字符，支持中英文、数字</i>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="settingClient.confirmPassword"
            maxlength="14"
            type="password"
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="oprationBtn">
      <div class="submit" @click="submitForm(ruleFormRef)">提交</div>
      <div class="reset" @click="resetForm(ruleFormRef)">重置</div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, getCurrentInstance, reactive, ref } from "vue";
import { SettingClienType, SettingInfoType } from "../views/Setting/types";
import { ElMessage, FormInstance } from "element-plus";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
const { $api } = getCurrentInstance()!.appContext.config.globalProperties!;
const store = useStore();
const router = useRouter();
//密码确认校验
const validateconfirmPassword = (rule: any, value: string, callback: any) => {
  // if (value == "") callback(new Error("确认密码不能为空!"));
  if (settingClient.password !== settingClient.confirmPassword) {
    callback(new Error("两次密码输入不一致!"));
  } else {
    callback();
  }
};
const rules = reactive({
  confirmPassword: [{ validator: validateconfirmPassword, trigger: "blur" }],
  password: [{ required: true, message: "密码不可为空", trigger: "blur" }],
});
const ruleFormRef = ref<FormInstance>();
const settingClient = reactive<SettingClienType>({
  password: "",
  confirmPassword: "",
});

//更新用户信息
const getUpdateUserInfo = async (id: number, data: SettingClienType) => {
  let res = await $api.user.reqUpdateUserInfo(id, data);
  if (res.code == 200) {
    ElMessage.success("更新成功！");
    router.push({ name: "Home" });
  }
};
let UserInfo = computed(() => store.state.userInfos);
//表单重置
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
//密码修改表单提交
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getUpdateUserInfo(store.state.userInfos.id, {
        password: settingClient.password,
      });
    } else {
      // console.log("error submit!");
      return false;
    }
  });
};
</script>
<style lang="less" scoped>
.passwordSetting {
  // height: 300px;
  border-bottom: 1px solid #f9f9f9;
  padding-top: 20px;

  .avatarBox {
    margin: 20px 0;

    .avatar-uploader {
      width: 100px;
      height: 100px;
      border-radius: 100px;
      background-color: #fafafa;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;
      position: relative;
      margin: 0 auto;

      .iconfont {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }

      .avatar {
        display: block;
        width: 100%;
        height: 100%;
        z-index: 10;
      }
    }
  }

  .InputArea {
    display: flex;
    justify-content: center;
    align-items: center;

    :deep(.el-form) {
      flex: 1;

      .el-form-item {
        .el-input {
          &:focus {
            border: 1px solid red !important;
          }

          .el-input__inner {
            background-color: #f0f1f4;
            border-radius: 10px;
          }
        }
      }

      .tips {
        font-size: 12px;
        color: #939393;
      }
    }
  }
}

.passwordSetting {
  .InputArea {
    margin-top: 20px;
    margin-bottom: 20px;
  }
}

.oprationBtn {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 50px;

  .submit,
  .reset {
    padding: 8px 25px;
    margin: 0 10px;
    border: 1px solid #ff8303;
    border-radius: 20px;
    font-size: 13px;
    cursor: pointer;

    &:hover {
      color: #fff;
      background-color: #ff8303;
    }
  }

  .reset {
    border: 1px solid #939393;

    &:hover {
      color: #fff;
      background-color: #939393;
    }
  }
}
</style>
